package com.tensquare.article.controller;

import com.tensquare.article.pojo.AL;
import com.tensquare.article.pojo.Article;
import com.tensquare.article.pojo.ArticleLabels;
import com.tensquare.article.service.ALService;
import com.tensquare.article.service.ArticleService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 控制器层
 *
 * @author Administrator
 */
@RestController
@CrossOrigin
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @RequestMapping(value = "/examine/{articleId}", method = RequestMethod.PUT)
    public Result examine(@PathVariable String articleId) {
        articleService.examine(articleId);
        return new Result(true, StatusCode.OK, "操作成功");
    }

    @RequestMapping(value = "/thumbup/{articleId}", method = RequestMethod.PUT)
    public Result updateThumbup(@PathVariable String articleId) {
        articleService.updateThumbup(articleId);
        return new Result(true, StatusCode.OK, "操作成功");
    }

    /**
     * 查询全部数据
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public Result findAll() {
        return new Result(true, StatusCode.OK, "查询成功", articleService.findAll());
    }

    /**
     * 根据ID查询
     *
     * @param id ID
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Result findById(@PathVariable String id) {
        return new Result(true, StatusCode.OK, "查询成功", articleService.findById(id));
    }


    /**
     * 分页+多条件查询
     *
     * @param searchMap 查询条件封装
     * @param page      页码
     * @param size      页大小
     * @return 分页结果
     */
    @RequestMapping(value = "/search/{page}/{size}", method = RequestMethod.POST)
    public Result findSearch(@RequestBody Map searchMap, @PathVariable int page, @PathVariable int size) {
        Page<Article> pageList = articleService.findSearch(searchMap, page, size);
        return new Result(true, StatusCode.OK, "查询成功", new PageResult<Article>(pageList.getTotalElements(), pageList.getContent()));
    }

    /**
     * 根据条件查询
     *
     * @param searchMap
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public Result findSearch(@RequestBody Map searchMap) {
        return new Result(true, StatusCode.OK, "查询成功", articleService.findSearch(searchMap));
    }

    @Autowired
    private ALService alService;
    /**
     * 增加
     *
     * @param articleLabels
     */
    @RequestMapping(method = RequestMethod.POST)
    public Result add(@RequestBody ArticleLabels articleLabels) {
        System.out.println("进来了：");
        Article article = articleLabels.getArticle();
        String[] pl = articleLabels.getLabels();
//        String token = (String) request.getAttribute("claims_user");
//        if (token == null || "".equals(token)) {
//            return new Result(false, StatusCode.ACCESSERROR, "权限不足，请重新登陆！");
//        }
        String id = articleService.add(article);
        AL al = null;
        for (int i = 0; i < pl.length; i++) {
            System.out.print("标签id：" + pl[i]);
            al = new AL();
            al.setLid(pl[i]);
            al.setAid(id);
            alService.save(al);
        }
        return new Result(true, StatusCode.OK, "增加成功");
    }

    /**
     * 修改
     *
     * @param article
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public Result update(@RequestBody Article article, @PathVariable String id) {
        article.setId(id);
        articleService.update(article);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    /**
     * 删除
     *
     * @param id
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public Result delete(@PathVariable String id) {
        articleService.deleteById(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    /**
     * 根据频道id查询文章
     *
     * @param channelid
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "/channel/{channelid}/{page}/{size}", method = RequestMethod.GET)
    public Result findByChannelid(@PathVariable String channelid, @PathVariable int page, @PathVariable int size) {
        Page<Article> pageList = articleService.findByChannelid(channelid, page, size);
        return new Result(true, StatusCode.OK, "查询成功", new PageResult<Article>(pageList.getTotalElements(), pageList.getContent()));
    }

}
